package com.yu.service.impl;

import com.yu.controller.dto.StuAdmissionData;
import com.yu.controller.dto.StuAge;
import com.yu.controller.dto.StuCount;
import com.yu.controller.dto.StuDateAnalysisModel;
import com.yu.mapper.StuScreenMapper;
import com.yu.service.StuScreenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service

public class StuScreenServiceImpl  implements StuScreenService {
    @Autowired
    private StuScreenMapper stuScreenMapper;
//    TODO service层获取排名前7的性别数据
    @Override
    public List<StuDateAnalysisModel> fnameSelect(int admissionyear,String academy) {
        List<StuDateAnalysisModel> stuFnames = stuScreenMapper.fnameSelect(admissionyear,academy);
//      // 将得到的百家姓排序
        Collections.sort(stuFnames,(a,b)->{
            return b.getValue()-a.getValue();
        });
        // 封装人数排名前7的姓氏，其余姓氏的统一为其他类型
        if(stuFnames.size()>7) {
            List<StuDateAnalysisModel> stuFnames1 = stuFnames.subList(0, 7);
            int count = 0;
            for (int i = 7; i < stuFnames.size(); i++) {
                count+=stuFnames.get(i).getValue();
            }
            StuDateAnalysisModel stuFname = new StuDateAnalysisModel();
            stuFname.setName("其他");
            stuFname.setValue(count);
            stuFnames1.add(stuFname);
            return stuFnames1;
        } else {
            return stuFnames;
        }
    }

    @Override
    public List<StuDateAnalysisModel> ageSelect(int admissionyear,String academy) {
        List<StuAge> stuAges = stuScreenMapper.ageSelect(admissionyear,academy);
        List<StuDateAnalysisModel> stuDateAnalysisModels =new ArrayList<StuDateAnalysisModel>();

        for (int i = 0; i < stuAges.size(); i++) {
            StuDateAnalysisModel stuDateAnalysisModel = new StuDateAnalysisModel();
            stuDateAnalysisModel.setName(stuAges.get(i).getName()+"岁");
            stuDateAnalysisModel.setValue(stuAges.get(i).getValue());
            stuDateAnalysisModels.add(stuDateAnalysisModel);
        }
        Collections.sort(stuDateAnalysisModels,(a,b)->{
            return b.getValue()-a.getValue();
        });
        return stuDateAnalysisModels;
    }

    @Override
    public List<StuDateAnalysisModel> sexSelect(int admissionyear,String academy) {
        List<StuDateAnalysisModel> stuDateAnalysisModels = stuScreenMapper.sexSelect(admissionyear,academy);
        return stuDateAnalysisModels;
    }

    @Override
    public List<StuDateAnalysisModel> majorSelect(int admissionyear,String academy) {
        List<StuDateAnalysisModel> majorSelect = stuScreenMapper.majorSelect(admissionyear,academy);
        return majorSelect;
    }

    @Override
    public List<List<String>> stuAdmissionDataSelect(int admissionyear,String academy) {
        List<StuAdmissionData> stuAdmissionData = stuScreenMapper.stuAdmissionDataSelect(admissionyear,academy);
        List<List<String>> stuAdmissionDataStringList = new ArrayList<List<String>>();
        List<String> stringList = null;
        for (int i = 0; i < stuAdmissionData.size(); i++) {
            stringList=new ArrayList<String>();
            stringList.add(stuAdmissionData.get(i).getName());
            stringList.add(stuAdmissionData.get(i).getMajor());
            stringList.add(stuAdmissionData.get(i).getAdmissiondate());
            System.out.println(stringList);
            stuAdmissionDataStringList.add(stringList);
        }
        return stuAdmissionDataStringList;
    }

    @Override
    public List<StuDateAnalysisModel> stuMapSelect(int admissionyear,String academy) {
        List<StuDateAnalysisModel> stuMapDatas = stuScreenMapper.stuMapSelect(admissionyear,academy);
        return stuMapDatas;
    }

    @Override
    public StuCount stuCountSelect(int admissionyear, String academy) {
        Integer totalCount = stuScreenMapper.totalCountSelect(admissionyear, academy);
        Integer admissionCount = stuScreenMapper.admissionCountSelect(admissionyear,academy);
        Double admissionRateTemp = (double)admissionCount/totalCount;
        // 保留三位小数
        String  str = String.format("%.2f",admissionRateTemp);
        double admissionRate = Double.parseDouble(str);

        return new StuCount(totalCount,admissionCount,admissionRate);
    }
}
